package com.zjy.software.day17;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class TypeDemo {
    TypeDemo(String name){
        System.out.println("我是有参构造器");
    }

    TypeDemo(){
        System.out.println("我是无参构造器");
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
//        getClassLoader();

//        getClassByThree();

        Class typeDemoClass = TypeDemo.class;
        Object o = typeDemoClass.newInstance();//代替了new关键字
        System.out.println("o = " + o);

        //演示通过classLoaer器获取资源文件
        InputStream resourceAsStream = typeDemoClass.getClassLoader().getResourceAsStream("config.properties");
        System.out.println("resourceAsStream = " + resourceAsStream);
        Properties propertiesDemo = new Properties();
        propertiesDemo.load(resourceAsStream);
        System.out.println("propertiesDemo.getProperty(\"name\") = " + propertiesDemo.getProperty("name"));
        resourceAsStream.close();
    }

    private static void getClassByThree() throws ClassNotFoundException {
        //第一种  获取类加载器,类.class
        Class typeDemoClass = TypeDemo.class;
//不同类型都有各自的Class对象,但是同一类型的Class对象是一样

        Class<Integer> integerClass = int.class;
        Class<Void> voidClass = void.class;
        Class<Boolean> booleanClass = boolean.class;
        Class classClass = Class.class;
        Class<byte[]> arrayClass = byte[].class;
        Class<byte[][]> arrayBypeaClass1 = byte[][].class;

//       //第二种,通过对象获取 对象.getClass()
        TypeDemo typeDemo = new TypeDemo("test");
        Class aClass = typeDemo.getClass();

        //第三种,通过字符串来获取一个类  Class.forName("类的全限定名")
        Class<?> aClass1 = Class.forName("com.zjy.software.day17.TypeDemo");
        System.out.println("aClass1 = " + aClass1);
    }

    private static void getClassLoader() {
        TypeDemo typeDemo = new TypeDemo("test");
        Class aClass = typeDemo.getClass();
        ClassLoader classLoader = aClass.getClassLoader();
        System.out.println("classLoader = " + classLoader);
        System.out.println("classLoader.getClass().getClassLoader() = " + classLoader.getClass().getClassLoader());
    }
}